import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;


public class CryptoChessTester {

	final static private String CRYPTO_CHESS_UUID = "{58e9b2b7-b74d-4ccb-ba76-89f757a32472}";
	final static private int LISTEN_PORT = 19725;
	final static private int OUT_PORT = 19726;
	
	public static void main(String[] args) throws Exception {
		InetAddress broadcastGroup = InetAddress.getByName("255.255.255.255");
		User user = new User("ComputerUser");
		byte[] announceBuf = (CRYPTO_CHESS_UUID + ";" + Request.announce.ordinal() + ";" + user.toString() + ";").getBytes();
		DatagramPacket announcePacket = new DatagramPacket(announceBuf, 0, announceBuf.length, broadcastGroup, LISTEN_PORT);
		byte[] announceReplyBuf = (CRYPTO_CHESS_UUID + ";" + Request.announceReply.ordinal() + ";" + user.toString()).getBytes();
		DatagramPacket announceReplyPacket = new DatagramPacket(announceReplyBuf, 0, announceReplyBuf.length, broadcastGroup, LISTEN_PORT);
		byte[] removeBuf = (CRYPTO_CHESS_UUID + ";" + Request.leave.ordinal() + ";" + user.toString() + ";").getBytes();
		DatagramPacket removePacket = new DatagramPacket(removeBuf, 0, removeBuf.length, broadcastGroup, LISTEN_PORT);
		byte[] acceptBuf = (CRYPTO_CHESS_UUID + ";" + Request.accept.ordinal() + ";" + user.toString() + ";").getBytes();
		DatagramPacket acceptPacket = new DatagramPacket(acceptBuf, 0, acceptBuf.length, broadcastGroup, LISTEN_PORT);
		byte[] playBuf = (CRYPTO_CHESS_UUID + ";" + Request.play.ordinal() + ";" + user.toString() + ";").getBytes();
		DatagramPacket playPacket = new DatagramPacket(playBuf, 0, playBuf.length, broadcastGroup, LISTEN_PORT);
		
		DatagramSocket s = new DatagramSocket(19275);
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		
		System.out.println("CryptoChess Tester");
		while(true) {
			System.out.println("\n\nAnnounce = a, Remove = r, Play = p, Accept = c, Announce Reply = n");
			
			char ch = 'a';
			ch = in.readLine().charAt(0);
			
			if(ch == 'a') {
				s.send(announcePacket);
			}
			else if (ch == 'r') {
				s.send(removePacket);
			}
			else if(ch == 'p') {
				s.send(playPacket);
			}
			else if(ch == 'c') {
				s.send(acceptPacket);
			}
			else if(ch == 'n') {
				s.send(announceReplyPacket);
			}
		}
	}
}
